package com.wentao.mall.exception.handler;

import com.wentao.mall.commons.Result;
import com.wentao.mall.exception.MallException;
import com.wentao.mall.exception.MallExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler({MallException.class})
    public Result mallException(MallException exception) {
        log.error(exception.getMsg());
        return Result.error(exception.getStatus(), exception.getMsg());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result methodArgumentNotValidException(MethodArgumentNotValidException exception) {
        List<String> errors = new ArrayList<>();
        List<ObjectError> allErrors = exception.getAllErrors();
        for (ObjectError objectError : allErrors) {
            errors.add(objectError.getDefaultMessage());
        }
        log.error(errors.toString());
        if (errors.size() == 0) {
            return Result.error(MallExceptionEnum.PARAMETER_ERROR);
        } else {
            return Result.error(MallExceptionEnum.PARAMETER_ERROR.getStatus(), errors.toString());
        }
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public Result constraintViolationException(ConstraintViolationException e){
        //1.将所有错误信息添加到list中
        List<String> list = new ArrayList<String>();
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        for(ConstraintViolation constraintViolation : constraintViolations){
            list.add(constraintViolation.getMessage());
        }
        //2.返回json错误数据类型
        if(list.size()==0){
            return Result.error(MallExceptionEnum.PARAMETER_ERROR);
        }else {
            return Result.error(MallExceptionEnum.PARAMETER_ERROR.getStatus(),list.toString());
        }
    }

//    @ExceptionHandler(Exception.class)
//    public Result exception(Exception exception){
//        log.error(exception.getMessage());
//        return Result.error(MallExceptionEnum.SYSTEM_ERROR);
//    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Result missingServletRequestParameterException(MissingServletRequestParameterException e){
        log.error(e.getMessage());
        return Result.error(MallExceptionEnum.PARAMETER_ERROR);
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
        log.error(e.getMessage());
        return Result.error(MallExceptionEnum.METHOD_ILLEGAL);
    }
}
